package org.glandais.birt.osgi.runner;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.glandais.birt.osgi.Activator;
import org.osgi.framework.Bundle;


/**
 * The Class ReportJob.
 */
public class ReportJob extends Job {

	/** The report configurator. */
	private ReportConfigurator reportConfigurator;

	/** The report stream. */
	private InputStream reportStream;

	/**
	 * Instantiates a new report job.
	 * 
	 * @param name
	 *            the name
	 * @param reportBundle
	 *            the report bundle
	 * @param reportPath
	 *            the report path
	 * @param reportConfigurator
	 *            the report configurator
	 * 
	 * @throws ReportException
	 *             the report exception
	 */
	public ReportJob(String name, Bundle reportBundle, String reportPath,
			ReportConfigurator reportConfigurator) throws ReportException {
		super(name);
		this.reportConfigurator = reportConfigurator;
		init(reportBundle, reportPath);
	}

	/**
	 * Instantiates a new report job.
	 * 
	 * @param name
	 *            the name
	 * @param reportURL
	 *            the report url
	 * @param reportConfigurator
	 *            the report configurator
	 * 
	 * @throws ReportException
	 *             the report exception
	 */
	public ReportJob(String name, URL reportURL,
			ReportConfigurator reportConfigurator) throws ReportException {
		super(name);
		this.reportConfigurator = reportConfigurator;
		init(reportURL);
	}

	/**
	 * Instantiates a new report job.
	 * 
	 * @param name
	 *            the name
	 * @param reportStream
	 *            the report stream
	 * @param reportConfigurator
	 *            the report configurator
	 * 
	 * @throws ReportException
	 *             the report exception
	 */
	public ReportJob(String name, InputStream reportStream,
			ReportConfigurator reportConfigurator) throws ReportException {
		super(name);
		this.reportConfigurator = reportConfigurator;
		this.reportStream = reportStream;
	}

	/**
	 * Inits the.
	 * 
	 * @param reportBundle
	 *            the report bundle
	 * @param reportPath
	 *            the report path
	 * 
	 * @throws ReportException
	 *             the report exception
	 */
	private void init(Bundle reportBundle, String reportPath)
			throws ReportException {
		init(reportBundle.getResource(reportPath));
	}

	/**
	 * Inits the.
	 * 
	 * @param resource
	 *            the resource
	 * 
	 * @throws ReportException
	 *             the report exception
	 */
	private void init(URL resource) throws ReportException {
		try {
			reportStream = resource.openStream();
		} catch (IOException e) {
			throw new ReportException(e);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	protected IStatus run(IProgressMonitor monitor) {
		try {
			OSGiBirtRunner.runReport(reportStream, reportConfigurator);
		} catch (ReportException e) {
			return new Status(Status.ERROR, Activator.PLUGIN_ID,
					Messages.BirtRunner_ERROR_MESSAGE, e);
		}
		return Status.OK_STATUS;
	}

}
